package controller;

import pojo.OrderDetail;
import service.IOrderService;
import service.IUserService;
import service.impl.OrderServiceImpl;
import service.impl.UserServiceImpl;
import utils.NoLoginUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "OrderListServlet", value = "/order-list")
public class OrderListServlet extends HttpServlet {
    private IUserService userService;
    private IOrderService orderService;

    @Override
    public void init() throws ServletException {
        userService=new UserServiceImpl();
        orderService=new OrderServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        NoLoginUtil.doVerify(request);
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("username");
        if (username == null) {
            request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
            return;
        }
        int uid = userService.queryIdByUsername(username);
        List<OrderDetail> orderDetails = orderService.queryOrderDetailsByUid(uid);
        request.setAttribute("orderDetails",orderDetails);
        request.getRequestDispatcher("/WEB-INF/order_list.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
